@extends('admin.common.main')

@section('cnt')
    <style>
        .permission-list > dd > dl > dd {
            margin-left: 0px;
        }
        label.error {
            left: 18px;
            font-size: 14px;
        }
    </style>
    <article class="page-container">
        <form action="{{ route('admin.role.auth', $role) }}" method="post" class="form form-horizontal" id="form-admin-role-add">
            @csrf
            <div class="row cl">
                <div class="formControls col-xs-12 col-sm-12">
                @foreach($authAll as $key=>$item)
                    <dl class="permission-list">
                        @if($item['pid'] == 0)
                        <dt>
                            <label>
                                <input type="checkbox" value="{{ $item['id'] }}" {{ $key==0 ? 'required' : '' }} name="auth[]"  @if(in_array($item['id'],$authes)) checked @endif>{{ $item['name'] }}
                            </label>
                        </dt>
                        @if(!empty($item['sub']))
                        <dd>
                            <dl class="cl permission-list2">
                                <dd>
                                    @foreach($item['sub'] as $val)
                                    <label class="">
                                        <input type="checkbox" value="{{ $val['id'] }}" name="auth[]"  @if(in_array($val['id'],$authes)) checked @endif>{{ $val['name'] }}
                                    </label>
                                    @endforeach              
                                </dd>
                            </dl>
                        </dd>
                        @endif
                        @endif
                    </dl>
                    @endforeach
                </div>
            </div>
            <div class="row cl _error">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <button type="submit" class="btn btn-success radius" id="admin-role-save" name="admin-role-save"><i class="icon-ok"></i> 确定</button>
                </div>
            </div>
        </form>
    </article>
@endsection

@section('js')
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
$(function(){
	$(".permission-list dt input:checkbox").click(function(){
		$(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
	});
	$(".permission-list2 dd input:checkbox").click(function(){
		var l =$(this).parent().parent().find("input:checked").length;
		var l2=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
		if($(this).prop("checked")){
			$(this).closest("dl").find("dt input:checkbox").prop("checked",true);
			$(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
		}
		else{
			if(l==0){
				$(this).closest("dl").find("dt input:checkbox").prop("checked",false);
			}
			if(l2==0){
				$(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
			}
		}
	});
	
	$("#form-admin-role-add").validate({
        errorPlacement: function (error, element) { //指定错误信息位置
            if (element.is(':radio') || element.is(':checkbox')) { //如果是radio或checkbox
            var eid = element.attr('name'); //获取元素的name属性
            error.appendTo($('._error')); 
            //默认
            // error.appendTo(element.parent()); //将错误信息添加当前元素的父结点后面
            } else {
                error.insertAfter(element);
            }
        },
		onkeyup:false,
		// focusCleanup:true,   清除错误，不注释就不会显示错误信息，
		success:"valid",
		submitHandler:function(form){
			$(form).ajaxSubmit({
                success: function(data){
                    if(data.status == 0){
                        layer.msg(data.msg,{icon:1,time:1000});
                        var index = parent.layer.getFrameIndex(window.name);
                        setTimeout(function(){
                            parent.layer.close(index);
                        },1000);
                    }
                }
            });
		}
	});
});
</script>
@endsection

